package cn.zyl.watchfaceblackfashion;

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.tencent.tws.watchfaceimpl.IWatchface;
import com.tencent.tws.watchfacelib.ZylUtils;

import java.util.List;

public class FashionFace
  extends IWatchface
{

  private Context mContext;
  private FashionBaseView mFashionBaseView;
  public static int cBrightness = 255;  //当前屏幕亮度
  public static boolean changeMySelf = false;  //是否自己改变的亮度
  private ContentObserver observer;  //亮度变化监控

  public void dozeModeEntered(IWatchface.ICallbackFinished paramICallbackFinished)
  {
    Log.d("zyltest","dozeModeEntered"+mFashionBaseView);
    if (this.mFashionBaseView != null)
    {
      this.mFashionBaseView.stopHandler();
      this.mFashionBaseView.unregistBroadcast();
      if (this.mFashionBaseView.showWeather)
        this.mFashionBaseView.unbindLBSService();
        if (this.mFashionBaseView.showStep)
            this.mFashionBaseView.unbindHealthKitService();
      this.mFashionBaseView.dozeEntered(paramICallbackFinished);
    }
    super.dozeModeEntered(paramICallbackFinished);
  }
  
  public void dozeModeExited(IWatchface.ICallbackFinished paramICallbackFinished)
  {
    Log.d("zyltest","dozeModeExited"+mFashionBaseView);
    if (this.mFashionBaseView != null)
    {
      this.mFashionBaseView.startHandler();
      this.mFashionBaseView.registBroadcast();
      if (this.mFashionBaseView.showWeather)
        this.mFashionBaseView.bindLBSService();
      if (this.mFashionBaseView.showStep)
        this.mFashionBaseView.bindHealthKitService();
      this.mFashionBaseView.dozeExited(paramICallbackFinished);
    }
    super.dozeModeExited(paramICallbackFinished);
  }
  
  public void dozeModeUpdate(IWatchface.ICallbackFinished paramICallbackFinished)
  {
    Log.d("zyltest","dozeModeUpdate"+mFashionBaseView);
    if (this.mFashionBaseView != null) {
      this.mFashionBaseView.dozeUpdate(paramICallbackFinished);
    }
    super.dozeModeUpdate(paramICallbackFinished);
  }
  
  public View getWatchfaceView(Context paramContext)
  {
    StackTraceElement[] aaa = Thread.currentThread().getStackTrace();
    for (StackTraceElement aa:aaa){
      //tos的ScreenService有bug，我先把它处理掉。这个问题解决了真是太开心了。。。2016.08.24
      if (aa.getClassName().contains("com.tencent.tws.launcher.ScreenService")){
        return null;
      }
    }
    Log.d("zyltest","getWatchfaceView"+mFashionBaseView);
    mContext = paramContext;
    if (this.mFashionBaseView == null) {
      this.mFashionBaseView = new FashionBaseView(paramContext);
    }
    return this.mFashionBaseView;
  }

  public void onPause()
  {
    Log.d("zyltest","onPause"+mFashionBaseView);
    if (this.mFashionBaseView != null) {
//        //如果关屏了，就强制打开屏试试
      if(this.mFashionBaseView.wakeLock!=null&&this.mFashionBaseView.wakeLock.isHeld()) //如果锁释放了，那就不自动亮屏吧...防止烧屏，并且可以省电
        this.mFashionBaseView.zylScreenOnAlways();
//      this.mFashionBaseView.zylScreenReset();
      this.mFashionBaseView.stopHandler();
    }
  }
  
  public void onResume()
  {
    Log.d("zyltest","onResume"+mFashionBaseView);
    if (this.mFashionBaseView != null) {
      if (this.mFashionBaseView.wakeLock!=null&&!this.mFashionBaseView.wakeLock.isHeld()){
        this.mFashionBaseView.zylScreenOnAlways();  //如果长亮锁被释放了，那么手动亮屏后再次触发长亮锁
      }
      this.mFashionBaseView.startHandler();
    }
  }
  
  public void onStart()
  {
    if (mContext!=null){
      //亮度监听我写这里吧。。。
      try{
        cBrightness = ZylUtils.getScreenBrightness(mContext);  //获取当前亮度
        Log.d("zyltest 当前亮度",cBrightness+"");
        observer = new ContentObserver(new Handler()) {
          @Override
          public void onChange(boolean selfChange) {
            Log.d("zyltest","2、Brightness Change..."+changeMySelf);
//            StackTraceElement[] aaa = Thread.currentThread().getStackTrace();
//            for (StackTraceElement aa:aaa){
//              Log.e("zyltest",aa.getClassName());
//            }
            if (!changeMySelf){
//            changeMySelf = false;
//          }else{
              Log.d("zyltest","系统修改亮度喽...");
              cBrightness = ZylUtils.getScreenBrightness(mContext);  //获取当前亮度
              //如果修改了系统亮度的话，就重新进入dim模式试试看
              if (FashionFace.this.mFashionBaseView != null) {
                FashionFace.this.mFashionBaseView.isDimRunning = false;  //退出dim再重新等待进入试试看
                FashionFace.this.mFashionBaseView.startHandler();
              }
            }
            changeMySelf = false;
            Log.d("zyltest","当前默认亮度："+cBrightness);
          }
        };
        mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true, observer);
      }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(mContext,"亮度获取失败。。",Toast.LENGTH_SHORT).show();
      }
    }

    Log.d("zyltest","onStart"+mFashionBaseView);
    if (this.mFashionBaseView != null)
    {
      this.mFashionBaseView.loadBitmapRes();
      this.mFashionBaseView.checkBatteyStatus();
      this.mFashionBaseView.registBroadcast();
      if (this.mFashionBaseView.showWeather)
        this.mFashionBaseView.bindLBSService();
      if (this.mFashionBaseView.showStep)
        this.mFashionBaseView.bindHealthKitService();
      this.mFashionBaseView.resetDozeMode();
    }
  }


  public void onStop()
  {
    if (mContext!=null&&observer!=null){
      mContext.getContentResolver().unregisterContentObserver(observer);
    }
    Log.d("zyltest","onStop"+mFashionBaseView);
    if (this.mFashionBaseView != null)
    {
      try{
        changeMySelf = false;
        ZylUtils.saveScreenBrightness(mContext,cBrightness);
      }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(mContext,"亮度恢复失败。。",Toast.LENGTH_SHORT).show();
      }
      this.mFashionBaseView.zylScreenReset();
//      Intent intent = new Intent();
//      intent.setAction("cn.zyl.watchface.change.stop");
//      mContext.sendBroadcast(intent);
      this.mFashionBaseView.unregistBroadcast();
      if (this.mFashionBaseView.showWeather)
        this.mFashionBaseView.unbindLBSService();
      if (this.mFashionBaseView.showStep)
        this.mFashionBaseView.unbindHealthKitService();
      this.mFashionBaseView.unloadBitmapRes();
    }
  }
}


/* Location:              E:\zyl\ylwork\Android\Android_Decode\apk2java\apktool2.1.0\WatchFaceBlackFashion\classes-dex2jar.jar!\com\example\watchfaceblackfashion\FashionFace.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */